package com.wdd.atcrowdfunding.manager.controller;


import com.wdd.atcrowdfunding.bean.Permission;

import com.wdd.atcrowdfunding.bean.User;
import com.wdd.atcrowdfunding.manager.service.PermissionService;
import com.wdd.atcrowdfunding.manager.service.UserService;
import com.wdd.atcrowdfunding.util.AjaxResult;
import com.wdd.atcrowdfunding.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;
    @Autowired
    private UserService userService;



    @RequestMapping("/index")
    public String index(){
        return "permission/index";
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "permission/add";
    }


    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id,Map map){
        Permission permission = permissionService.getPermissionById(id);
        map.put("permission",permission);
        return "permission/update";
    }


    /**
     * 显示许可树
     * @return
     */
    @ResponseBody
    @RequestMapping("/loadData")
    public AjaxResult loadData(){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            List<Permission> root = new ArrayList<Permission>();
            List<Permission> childredPermissons =  permissionService.queryAllPermission();
            Map<Integer,Permission> map = new HashMap<Integer,Permission>();//100
            for (Permission innerpermission : childredPermissons) {
                map.put(innerpermission.getId(), innerpermission);
            }
            for (Permission permission : childredPermissons) { //100
                //通过子查找父
                //子菜单
                Permission child = permission ; //假设为子菜单
                if(child.getPid() == null ){
                    root.add(permission);
                }else{
                    //父节点
                    Permission parent = map.get(child.getPid());
                    parent.getChildren().add(child);
                }
            }
            ajaxResult.setSuccess(true);
            ajaxResult.setData(root);
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("加载许可树数据失败!");
        }


        return ajaxResult ;
    }

    /**
     * 添加许可
     * @param permission
     * @return
     */
    @ResponseBody
    @RequestMapping("/doAdd")
    public AjaxResult doAdd(Permission permission,HttpSession session){
        AjaxResult ajaxResult = new AjaxResult();

        try {

            int count = permissionService.savePermission(permission);
            if(count==1){
                ajaxResult.setSuccess(true);
                //查询已拥有的许可，放入session，在界面左侧显示
                PermissionRoot(session);
            }
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("保存许可树数据失败!");
        }


        return ajaxResult ;
    }

    /**
     * 修改许可
     * @param permission
     * @return
     */
    @ResponseBody
    @RequestMapping("/doUpdate")
    public AjaxResult doUpdate(Permission permission,HttpSession session){
        AjaxResult ajaxResult = new AjaxResult();
        System.out.println(permission);
        try {
            int count = permissionService.updatePermission(permission);

            ajaxResult.setSuccess(true);

            //查询已拥有的许可，放入session，在界面左侧显示
            PermissionRoot(session);

        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("保存许可树数据失败!");
        }


        return ajaxResult ;
    }

    /**
     * 删除许可
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/deletePermission")
    public AjaxResult deletePermission(Integer id, HttpSession session){
        AjaxResult ajaxResult = new AjaxResult();

        try {
            int count = permissionService.deletePermission(id);
            if(count==1){
                ajaxResult.setSuccess(true);

                //查询已拥有的许可，放入session，在界面左侧显示
                PermissionRoot(session);
            }

        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("删除许可树数据失败!");
        }


        return ajaxResult ;
    }


    /**
     * 查询已拥有的许可，放入session，在界面左侧显示
     */
    public void PermissionRoot(HttpSession session){
        //查询已拥有的许可，放入session，在界面左侧显示
        User user = (User) session.getAttribute(Const.LOGIN_USER);
        List<Permission> myPermissions = userService.queryPermissionByUserid(user.getId());
        Permission permissionRoot = null;
        Map<Integer,Permission> map = new HashMap<Integer,Permission>();
        Set<String> myUris = new HashSet<String>(); // 用于拦截器拦截许可权限
        for (Permission innerpermission : myPermissions) {
            map.put(innerpermission.getId(), innerpermission);
            myUris.add("/"+innerpermission.getUrl());
        }
        for (Permission pe : myPermissions) { //100
            //通过子查找父
            //子菜单
            Permission child = pe ; //假设为子菜单
            if(child.getPid() == null ){
                permissionRoot = pe;
            }else{
                //父节点
                Permission parent = map.get(child.getPid());
                parent.getChildren().add(child);
            }
        }
        session.setAttribute("permissionRoot",permissionRoot);
        session.setAttribute(Const.MY_URIS,myUris);
        //end
    }

}
